
document.addEventListener('DOMContentLoaded', () => {
    const form = document.getElementById('loginForm');
    const errorMsg = document.getElementById('errorMsg');

    form.addEventListener('submit', async (e) => {
        e.preventDefault();

        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        const rememberMe = document.getElementById('rememberMe').checked;

        // 清空错误提示
        errorMsg.textContent = '';

        try {
            const response = await fetch('/user/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: new URLSearchParams({
                    username,
                    password,
                    rememberMe
                })
            });

            const result = await response.json();

            if (result.code === 0) {
                // 登录成功跳转
                window.location.href = '/user/index';
            } else {
                errorMsg.textContent = result.msg || '登录失败，请重试';
            }
        } catch (error) {
            errorMsg.textContent = '网络错误，请检查连接';
            console.error('登录请求失败:', error);
        }
    });
});
